const db=wx.cloud.database();
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    starttime:'',
    endtime:'',
    starttime1:'',
    endtime1:'',
    show: false,
    showtishi:false,
    id:'',
    tixing:'',
    currentDate: '12:00',
    minHour: 0,
    maxHour: 24,
    taskname:'',
    taskdetails:'',
    tasktarget:'',
    targetnum:0,
    jieshouzuzhi:'',
    showgroupser:false,
    userid:'',
    groupid:[],
    actions: [],
    groupmember:[],
    result: ['成员可查看完成情况','成员可发表评论','成员可上传附件'],
  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  onserch(){
    let that=this;
    this.setData({
      showgroupser:true
    })
    db.collection("group").where({
      masterid:this.data.userid
    }).get().then(res=>{
      for(var index in res.data){
        that.setData({
          groupid:that.data.groupid.concat({id:res.data[index]._id,name:res.data[index].groupname}),
          actions:that.data.actions.concat({name:res.data[index].groupname})
        })
        
      }
    })
  },
  closegroupser() {
    this.setData({ showgroupser: false });
  },

  Selectgroupser(event) {
    
    this.setData({
      jieshouzuzhi:event.detail.name
    })
    var id;
    for(var index in this.data.groupid)
        if(this.data.groupid[index].name==event.detail.name)
          id=this.data.groupid[index].id;
    db.collection("group").doc(id).get().then(res=>{
      for(var index in res.data.groupmember)
        this.setData({
          groupmember:this.data.groupmember.concat(res.data.groupmember[index].userid)
        })
    })
  },
  onInput(event) {
    this.setData({
      tixing:"",
    })
    this.setData({
      currentDate: event.detail,
    });
    this.setData({
      tixing:this.data.tixing.concat(this.data.currentDate),
      showtishi:false
    })
  },
  onselect() {
    this.setData({ showtishi: true });
  },

  onClosetishi() {
    this.setData({ showtishi: false });
  },
  start() {
    this.setData({ 
      show: true, 
      id:"1" 
    });

  },
  end(){
    this.setData({ 
      show: true,
      id:"2"
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
    
  },
  onConfirm(event) {
    if(this.data.id=='1'){
      this.setData({
        starttime:"",
      })
  
      this.setData({
        show: false,
      });
      this.setData({
        starttime:event.detail,
        starttime1:this.data.endtime.concat(this.formatDate(event.detail))
      })
    }else{
      this.setData({
        endtime:"",
      })
      this.setData({
        show: false,
      });
      this.setData({
        endtime1:this.data.endtime.concat(this.formatDate(event.detail)),
        endtime:event.detail
      })
    }
    
    
  },

  chuancan(){
    if(this.data.taskname==''){
      wx.showToast({
        title: '任务名不为空',
        icon:"error"
      })
    }else if(this.data.starttime==''){
      wx.showToast({
        title: '开始时间不为空',
        icon:"error"
      })
    }else if(this.data.endtime==''){
      wx.showToast({
        title: '结束时间不为空',
        icon:"error"
      })
    }else if(this.data.tasktarget==''){
      wx.showToast({
        title: '标签不为空',
        icon:"error"
      })
    }else if(this.data.jieshouzuzhi==''){
      wx.showToast({
        title: '请选择一个组织',
        icon:"error"
      })
    }else{
      let target=this.data.tasktarget.split(",");
      
      for(var index in this.data.groupmember){
        var addtask={"taskname":this.data.taskname,"taskpre":0,target:target,"starttime":this.data.starttime,"endtime":this.data.endtime,"tasktargetnum":this.data.targetnum,"hid":"block","value":this.data.taskdetails,"allperson":this.data.groupmember.length,
      "finshedperson":0,  "connection":[],
      userid:this.data.groupmember[index] };
      console.log(addtask.starttime)
        wx.cloud.callFunction({
          name:"addgrouptask",
          data:{
            openid:this.data.groupmember[index],
            task:addtask,
            starttime:addtask.starttime,
            endtime:addtask.endtime
          }
        }).then(res=>{
          console.log(res)
        })
       
      }
       wx.switchTab({
          url: '../task/task',
        })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>{
      this.setData({
        userid:res.result.openid
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})